package com.ruoyi.order.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.apache.poi.ss.formula.functions.Na;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.TbOrderMapper;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;

/**
 * 订单列表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-02
 */
@Service
public class TbOrderServiceImpl implements ITbOrderService {
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 查询订单列表
     *
     * @param orderId 订单列表主键
     * @return 订单列表
     */
    @Override
    public TbOrder selectTbOrderByOrderId(Long orderId) {
        return tbOrderMapper.selectTbOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表列表
     *
     * @param tbOrder 订单列表
     * @return 订单列表
     */
    @Override
    public List<TbOrder> selectTbOrderList(TbOrder tbOrder) {
        return tbOrderMapper.selectTbOrderList(tbOrder);
    }

    /**
     * 新增订单列表
     *
     * @param tbOrder 订单列表
     * @return 结果
     */
    @Override
    public int insertTbOrder(TbOrder tbOrder) {
        tbOrder.setCreateTime(DateUtils.getNowDate());
        return tbOrderMapper.insertTbOrder(tbOrder);
    }

    /**
     * 修改订单列表
     *
     * @param tbOrder 订单列表
     * @return 结果
     */
    @Override
    public int updateTbOrder(TbOrder tbOrder) {
        tbOrder.setUpdateTime(DateUtils.getNowDate());
        return tbOrderMapper.updateTbOrder(tbOrder);
    }

    /**
     * 批量删除订单列表
     *
     * @param orderIds 需要删除的订单列表主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByOrderIds(Long[] orderIds) {
        return tbOrderMapper.deleteTbOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单列表信息
     *
     * @param orderId 订单列表主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByOrderId(Long orderId) {
        return tbOrderMapper.deleteTbOrderByOrderId(orderId);
    }

    @Override
    public TableDataInfo seleOrderEs(TbOrder tbOrder, Integer vofi, Integer pageSize) {
        Integer pageNum = 0;
        if (vofi > 0) {
            pageNum = vofi - 1;
        }
        PageRequest of = PageRequest.of(pageNum, pageSize);
        BoolQueryBuilder builder = new BoolQueryBuilder();
        if (StringUtils.isNotEmpty(tbOrder.getOrderNo())) {
            builder.must(QueryBuilders.matchQuery("orderNo", tbOrder.getOrderNo()));
        }
        FieldSortBuilder orderId = SortBuilders.fieldSort("orderId").order(SortOrder.DESC);
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(of).withQuery(builder).withSorts(orderId);
        SearchHits<TbOrder> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), TbOrder.class);
        long totalHits = search.getTotalHits();
        ArrayList<TbOrder> tbOrders = new ArrayList<>();
        for (SearchHit<TbOrder> tbOrderSearchHit : search) {
            TbOrder content = tbOrderSearchHit.getContent();
            tbOrders.add(content);
        }

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(totalHits);
        tableDataInfo.setMsg("产讯成功");
        tableDataInfo.setRows(tbOrders);
        tableDataInfo.setCode(200);
        return tableDataInfo;
    }
}
